import initProcessEvents from "./config/ProcessEvents";
import ConfigureDesktopApps from "./config/ConfigureDesktopApps";
const electron = require('electron');
const { app,globalShortcut,dialog } = require('electron');
const BrowserWindow=electron.BrowserWindow;
const path = require('path');

let ipcMain= require('electron').ipcMain;

//用一个 Tray 来表示一个图标,这个图标处于正在运行的系统的通知区 ，通常被添加到一个 context menu 上.
const Menu = electron.Menu;
const Tray = electron.Tray;

if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`;

//托盘对象
var appTray = null;
var trayIcon=null;
let mainWindow;

function createWindow() {
    // Create the browser window.
    mainWindow = new BrowserWindow(ConfigureDesktopApps);
    //启用 electron-launch-page


    //主进程对接   vue页面连接
    mainWindow.loadURL(winURL);
    // mainWindow.once('read-top-show',()=>{
    //     mainWindow.show();
    // })
    ipcMain.on('on-window-top', (res) => {
        mainWindow.focus();
    });

    mainWindow.on('closed', () => {
        mainWindow = null
    });
    mainWindow.on('ready-to-show', () => {
        mainWindow.show();
    });

    //文件以及文件夹弹框
    ipcMain.on('open-directory-dialog', function (event,p) {
        if(p=="openDirectory"){
            let options = {
                defaultPath:"C:\\",
                properties: [p],
            };
            dialog.showOpenDialog(mainWindow,options)
                .then(result=>{
                    if(result){// 如果有选中
                        // 发送选择的对象给子进程
                        event.sender.send('selectedItem', result.filePaths[0]);
                    }
                })
        }else{
            let options = {
                defaultPath:"C:\\",
                properties: [p],
            };
            dialog.showOpenDialog(mainWindow,options)
                .then(result=>{
                    if(result){// 如果有选中
                        // 发送选择的对象给子进程
                        event.sender.send('selectedItem', result.filePaths[0]);
                    }
                })
        }

    });

    // 打开调试窗口(默认打开)
    // 关闭调试窗口
    // mainWindow.webContents.closeDevTools();
    // 初始化进程之间事件监听
    global.mainWindow = mainWindow;

    // mainWindow.webContents.openDevTools();

    initProcessEvents();

    //注册快捷键
    globalShortcut.register('CommandOrControl+Shift+k',(e)=>{
        mainWindow.webContents.openDevTools();
    });

    //系统托盘右键菜单
    var trayMenuTemplate = [
        {
            label: '退出',
            click: function () {
                //ipc.send('close-main-window');
                app.quit();
            }
        }
    ];

    //系统托盘图标目录
    trayIcon = path.join(__dirname, '');

    appTray = new Tray(path.join(__static, '/ux-testing-icon-64.ico'));

    //图标的上下文菜单
    const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);

    //设置此托盘图标的悬停提示内容
    appTray.setToolTip('uxtester');

    //设置此图标的上下文菜单
    appTray.setContextMenu(contextMenu);
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
let firstPath=null;
app.on('ready',()=> {
    createWindow();
});
